package com.bxmdm.ragdemo.controller;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.bxmdm.ragdemo.dto.QAPair;
import com.bxmdm.ragdemo.dto.SearchResult;
import com.bxmdm.ragdemo.dto.RagSearchResult;
import com.bxmdm.ragdemo.service.EnglishQAService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 英语问答系统的API接口
 */
@RestController
@RequestMapping("/api/english-qa")
@Tag(name = "英语问答", description = "英语学习问答系统的相关接口")
public class EnglishQAController {

    @Autowired
    private EnglishQAService englishQAService;

    @GetMapping("/semantic-search")
    @Operation(summary = "语义检索问答", description = "语义检索相关问答（RAG增强版）")
    public ResponseEntity<List<QAPair>> semanticSearch(
            @Parameter(description = "用户问题") @RequestParam("question") String question,
            @Parameter(description = "返回结果数量") @RequestParam(value = "topK", defaultValue = "5") int topK) {
        String decodedQuestion = URLDecoder.decode(question, StandardCharsets.UTF_8);
        List<QAPair> result = englishQAService.semanticSearch(decodedQuestion, topK);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/import")
    @Operation(summary = "导入问答数据", description = "从上传的JSON文件导入问答数据到向量数据库")
    public ResponseEntity<Map<String, Integer>> importFromFile(
            @Parameter(description = "JSON格式的问答数据文件") @RequestParam("file") MultipartFile file) {
        Map<String, Integer> result = englishQAService.importFromFile(file);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/import/local")
    @Operation(summary = "从本地文件导入", description = "从服务器本地路径导入问答数据")
    public ResponseEntity<Map<String, Integer>> importFromLocalFile(
            @Parameter(description = "服务器上的文件路径") @RequestParam("filePath") String filePath) {
        Map<String, Integer> result = englishQAService.importFromLocalFile(filePath);
        return ResponseEntity.ok(result);
    }

    // @GetMapping("/rag-search")
    // @Operation(summary = "RAG检索问答", description = "使用RAG技术从向量库检索相关问答对")
    // public ResponseEntity<SearchResult> ragSearch(
    // @Parameter(description = "用户问题") @RequestParam("question") String question,
    // @Parameter(description = "返回结果数量") @RequestParam(value = "topK", defaultValue
    // = "5") int topK) {
    // String decodedQuestion = URLDecoder.decode(question, StandardCharsets.UTF_8);
    // SearchResult result = englishQAService.ragSearch(decodedQuestion, topK);
    // return ResponseEntity.ok(result);
    // }

    // @GetMapping("/rag-ai-answer")
    // @Operation(summary = "RAG增强AI回答", description = "检索相关问答后，利用LLM综合生成最终答案")
    // public ResponseEntity<String> ragAiAnswer(
    // @Parameter(description = "用户问题") @RequestParam("question") String question,
    // @Parameter(description = "检索数量") @RequestParam(value = "topK", defaultValue =
    // "3") int topK) {
    // String decodedQuestion = URLDecoder.decode(question, StandardCharsets.UTF_8);
    // String result = englishQAService.ragAiAnswer(decodedQuestion, topK);
    // return ResponseEntity.ok(result);
    // }

    // @GetMapping("/semantic-summary")
    // @Operation(summary = "语义检索结果总结", description = "直接返回RAG检索结果的自然语言总结")
    // public ResponseEntity<String> semanticSearchSummary(
    // @Parameter(description = "用户问题") @RequestParam("question") String question,
    // @Parameter(description = "检索数量") @RequestParam(value = "topK", defaultValue =
    // "5") int topK) {
    // String decodedQuestion = URLDecoder.decode(question, StandardCharsets.UTF_8);
    // String result = englishQAService.semanticSearchSummary(decodedQuestion,
    // topK);
    // return ResponseEntity.ok(result);
    // }

    @GetMapping("/rag-answer")
    @Operation(summary = "RAG检索问答结果", description = "基于语义相似度检索相关问答并按分数排序返回格式化结果")
    public ResponseEntity<RagSearchResult> ragAnswer(
            @Parameter(description = "用户问题") @RequestParam("question") String question,
            @Parameter(description = "返回结果数量") @RequestParam(value = "topK", defaultValue = "5") int topK) {
        String decodedQuestion = URLDecoder.decode(question, StandardCharsets.UTF_8);
        RagSearchResult result = englishQAService.ragAnswer(decodedQuestion, topK);
        return ResponseEntity.ok(result);
    }
}